package cn.wolfcode.crmspringboot.util;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author yinzq
 */

public class GUID implements Serializable {

    private static final long serialVersionUID = -5979233231668276558L;

    private static final String RAND_CHARS = "0123456789abcdefghigklmnopqrstuvtxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";

    private static final int[] prefix = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    private static Random random = new Random();

    private static final int maxLength = 14;

    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};

    private static final String TRADE_CODE = "1";

    public static String get() {
        UUID uuid = UUID.randomUUID();
        StringBuffer sb = new StringBuffer();
        get(sb, uuid.getMostSignificantBits());
        get(sb, uuid.getLeastSignificantBits());
        return sb.toString();
    }

    public static String getRandom(int len) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < len; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }

    private static StringBuffer get(StringBuffer sb, long bits) {
        int count = 13;
        while (count-- > 0) {
            long low = bits & 0x1FL;
            if (low < 10L) {
                sb.append((char) ('0' + low));
            } else {
                sb.append((char) ('A' + (low - 10)));
            }
            bits >>>= 5;
        }
        return sb;
    }

    public static String getRandStr(int length, int randType) {
        StringBuffer hash = new StringBuffer(length);
        if (randType == 1) {
            for (int i = 0; i < length; i++){
                hash.append(RAND_CHARS.charAt(10 + random.nextInt(52)));
            }
        } else if (randType == 2) {
            for (int i = 0; i < length; i++) {
                hash.append(RAND_CHARS.charAt(random.nextInt(10)));
            }
        } else {
            for (int i = 0; i < length; i++) {
                hash.append(RAND_CHARS.charAt(random.nextInt(62)));
            }
        }
        return hash.toString();
    }

    public static String getPrefix() {
        return prefix[random.nextInt(prefix.length)] + "";
    }

    public static String getTradeCode() {
        return getDateTime() + getRandStr(3,2);
    }

    public static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    public static String getHourTime() {
        DateFormat sdf = new SimpleDateFormat("yyMMddHH");
        return sdf.format(new Date());
    }

    public static String toCode(Long merchantCode) {
        String idStr = merchantCode.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }

}
